package com.leonzx.base.config.pager;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 分页类
 *
 * @param <T>
 */
public class Page<T> implements Iterable<T>, Cloneable {
    protected int page = 1;
    protected int rows = 10;
    protected String orderBy;
    protected String orderDir;
    protected boolean countTotal = true;
    protected List<T> result = null;
    protected long totalCount = -1L;
    protected String jsonResult;
    public static final int PAGE_SIZE = 15;

    public Page() {
    }

    public Page(int page, int rows) {
        this.page = page;
        this.rows = rows;
    }

    public Page(int page, int rows, String orderBy, String orderDir) {
        this.page = page;
        this.rows = rows;
        this.orderBy = orderBy;
        this.orderDir = orderDir;
    }

    public int getPage() {
        return this.page;
    }

    public void setPage(int page) {
        this.page = page;
        if (page < 1) {
            this.page = 1;
        }

    }

    public int getRows() {
        return this.rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
        if (rows < 1) {
            this.rows = 1;
        }

    }

    public String getOrderBy() {
        return this.orderBy;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }

    public String getOrderDir() {
        return this.orderDir;
    }

    public void setOrderDir(String orderDir) {
        String lowcaseOrderDir = StringUtils.upperCase(orderDir);
        String[] orderDirs = StringUtils.split(lowcaseOrderDir, ',');
        String[] var4 = orderDirs;
        int var5 = orderDirs.length;
        for (int var6 = 0; var6 < var5; ++var6) {
            String orderDirStr = var4[var6];
            if (!StringUtils.equals("DESC", orderDirStr) && !StringUtils.equals("ASC", orderDirStr)) {
                throw new IllegalArgumentException("排序方向" + orderDirStr + "不是合法值");
            }
        }
        this.orderDir = lowcaseOrderDir;
    }

    public String getSortStr() {
        String sortStr = "";
        List sortList = this.getSort();
        for (int i = 0; i < sortList.size(); ++i) {
            Page.Sort sort = (Page.Sort) sortList.get(i);
            if (i != 0) {
                sortStr = sortStr + ",";
            }
            sortStr = sortStr + sort.getProperty() + " " + sort.getDir();
        }
        return sortStr;
    }

    public List<Sort> getSort() {
        ArrayList orders = new ArrayList();
        if (this.orderBy == null) {
            return orders;
        } else {
            String[] orderBys = StringUtils.split(this.orderBy, ',');
            String[] orderDirs = StringUtils.split(this.orderDir, ',');
            if (orderBys.length != orderDirs.length) {
                throw new IllegalArgumentException("分页多重排序参数中,排序字段与排序方向的个数不相等");
            } else {
                for (int i = 0; i < orderBys.length; ++i) {
                    orders.add(new Page.Sort(orderBys[i], orderDirs[i]));
                }

                return orders;
            }
        }
    }

    public boolean isOrderBySetted() {
        return StringUtils.isNotBlank(this.orderBy) && StringUtils.isNotBlank(this.orderDir);
    }

    public boolean isCountTotal() {
        return this.countTotal;
    }

    public void setCountTotal(boolean countTotal) {
        this.countTotal = countTotal;
    }

    public int getOffset() {
        return (this.page - 1) * this.rows;
    }

    public List<T> getResult() {
        return this.result;
    }

    public void setResult(List<T> result) {
         if (result instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page pageList = (com.github.pagehelper.Page) result;
            this.totalCount = pageList.getTotal();
            this.result = pageList.getResult();
        } else {
            this.result = result;
        }

    }

    public long getTotalCount() {
        return this.totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public Iterator<T> iterator() {
        return this.result.iterator();
    }

    public int getTotalPages() {
        return (int) Math.ceil((double) this.totalCount / (double) this.getRows());
    }

    public boolean hasNextPage() {
        return this.page + 1 <= this.getTotalPages();
    }

    public boolean isLastPage() {
        return !this.hasNextPage();
    }

    public int getNextPage() {
        return this.hasNextPage() ? this.getPage() + 1 : this.getPage();
    }

    public boolean hasPrePage() {
        return this.getPage() > 1;
    }

    public boolean isFirstPage() {
        return !this.hasPrePage();
    }

    public int getPrePage() {
        return this.hasPrePage() ? this.getPage() - 1 : this.getPage();
    }

    public List<Integer> getSlider() {
        byte count = 5;
        int halfSize = count / 2;
        int totalPage = this.getTotalPages();
        int startPageNo = Math.max(this.getPage() - halfSize, 1);
        int endPageNo = Math.min(startPageNo + count - 1, totalPage);
        if (endPageNo - startPageNo < count) {
            startPageNo = Math.max(endPageNo - count, 1);
        }
        ArrayList result = new ArrayList();
        for (int i = startPageNo; i <= endPageNo; ++i) {
            result.add(Integer.valueOf(i));
        }
        return result;
    }

    public static class Sort {
        public static final String ASC = "ASC";
        public static final String DESC = "DESC";
        private final String property;
        private final String dir;
        public Sort(String property, String dir) {
            this.property = property;
            this.dir = dir;
        }

        public String getProperty() {
            return this.property;
        }

        public String getDir() {
            return this.dir;
        }
    }
}

